#define _CRT_SECURE_NO_WARNINGS 1
#include"Queue.h"
void QueueInit(Queue* qu)
{
	assert(qu);
	qu->phead = NULL;
	qu->ptail = NULL;
	qu->size = 0;
}
void QueuePush(Queue* qu, QDataType x)
{
	assert(qu);
	QueueNode* node = (QueueNode*)malloc(sizeof(QueueNode));
	if (node == NULL)
	{
		perror("malloc fail!");
		exit(1);
	}
	node->data = x;
	node->next = NULL;
	if (qu->phead == NULL)
	{
		qu->phead = qu->ptail = node;
	}
	else
	{
		qu->ptail->next = node;
		qu->ptail = node;
	}
	qu->size++;
}
bool QueueEmpty(Queue* qu)
{
	assert(qu);
	return qu->phead == NULL && qu->ptail == NULL;
}
void QueuePop(Queue* qu)
{
	assert(qu);
	assert(!QueueEmpty(qu));
	if (qu->phead->next == NULL)
	{
		free(qu->phead);
		qu->phead = qu->ptail = NULL;
	}
	else
	{
		QueueNode* Next = qu->phead->next;
		free(qu->phead);
		qu->phead = Next;
	}
	qu->size--;
}
QDataType QueueFront(Queue* qu)
{
	assert(qu);
	assert(!QueueEmpty(qu));
	return qu->phead->data;
}
QDataType QueueBack(Queue* qu)
{
	assert(qu);
	assert(!QueueEmpty(qu));
	return qu->ptail->data;
}
int QueueSize(Queue* qu)
{
	assert(qu);
	return qu->size;
}
void QueueDestroy(Queue* qu)
{
	assert(qu);
	assert(!QueueEmpty(qu));
	if (qu->phead->next == NULL)
	{
		free(qu->phead);
		qu->phead = qu->ptail = NULL;
	}
	else
	{
		QueueNode* next = qu->phead->next;
		free(qu->phead);
		qu->phead = next;
	}
}